package kpi.javatraining6.gnatiuk.src.tester.user;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Set;

public class User implements Serializable {
	private static final long serialVersionUID = 1L;

	private String userName;
    private String password;
    private HashMap<String, UserProgress> userProgress;

    /**
     * constructor  for users
     */
    public User(String userName, String password){
        this.userName = userName;
        this.password = password;
        userProgress = new HashMap<>();
    }

    public User(String userName, String password, HashMap<String, UserProgress> userProgress){
        this(userName, password);
        this.userProgress = userProgress;
    }

    @Override
    public boolean equals(Object anObj) {
          if (anObj == null) {
              return false;
          }
          if (!(anObj instanceof User)) {
              return false;
          }
          User someUser = (User)anObj;
          return userName.equals(someUser.userName);
    }

    public UserProgress getCurrentProgress(String topic) {
        if (!userProgress.containsKey(topic)) {
            userProgress.put(topic, new UserProgress());
        }
        return userProgress.get(topic);
    }

    public Set<String> getStartedTopics() {
        return userProgress.keySet();
    }

    public String getStatistic(){
    	String statistic="";
    	Set<String> topics = userProgress.keySet();
    	for (String topic : topics) {
			statistic += String.format("%s: %s", topic, userProgress.get(topic).getStatistic());
		}
    	if(statistic.equals("")){
    		return "There is no statistical data.";
    	}else{
    		return statistic;
    	}
    }

    public String getUserName() {
        return userName;
    }

    public String getPassword() {
        return password;
    }

    @Override
    public String toString(){
        return String.format("User: %s %nPassword: %s",  userName, password);
    }


}
